<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //实现登陆注册
      let loginList = [
        { userName: 'admin', password: 'admin321' },
        { userName: 'zhangsan', password: 'zs321' },
        { userName: 'lisi', password: 'ls321' },
        { userName: 'yyqx', password: 'yyqx321' },
      ]

      let userInfo = {
        token1: {
          city: '杭州',
          money: '1000w',
          sex: '女',
          userName: 'admin',
          password: 'admin321',
        },
        token2: {
          city: '北京',
          money: '1100w',
          sex: '女',
          userName: 'zhangsan',
          password: 'zs321',
        },
        token3: {
          city: '北京',
          money: '800w',
          sex: '男',
          userName: 'lisi',
          password: 'ls321',
        },
        token4: {
          city: '天津',
          money: '600w',
          sex: '男',
          userName: 'yyqx',
          password: 'yyqx321',
        },
      }
      let renderlogin = function (loginList, userInfo) {
        while (true) {
          let inputName = prompt('请输入登陆用户名')
          let inputPassword = prompt('请输入登陆密码')
          for (let i = 0; i < loginList.length; i++) {
            if (
              inputName === loginList[i].userName &&
              inputPassword === loginList[i].password
            ) {
              for (let key in userInfo) {
                let num = Math.floor(Math.random() * 4) + 1
                let token = 'token' + num
                return alert(`用户登陆成功，具体用户信息:
                city:${userInfo[token].city}
                money: ${userInfo[token].money}
                sex: ${userInfo[token].sex}
                userName: ${userInfo[token].userName}
                `)
              }
            }
          }
          let num = +prompt(`
              账户不存在:
              1.注册账号
              2.退出
          `)
          if (num === 1) {
            let loginName = prompt('请输入注册账号')
            let loginPassword = prompt('请输入注册密码')
            let newUser = { userName: loginName, password: loginPassword }
            loginList.push(newUser)
            alert('注册成功，请重新登陆～')
          } else if (num === 2) {
            return
          } else {
            return alert('登陆失败，已无机会！')
          }
        }
      }
      renderlogin(loginList, userInfo)
    </script>
  </body>
</html>
